<?php
/**
 * 频道管理
 */
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\ChannelInfo;
use think\App;
use think\exception\ValidateException;

class Channel extends BaseController
{
    private $channelParam = [];

    public function __construct(App $app)
    {
        parent::__construct($app);
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->channelParam = $this->request->post([
                'id' => $this->request->post('id'),                  //id
                'channel_name' => trim($this->request->post('channel_name')),                  //*频道名称(星号为必填参数)
                'channel_cap' => $this->request->post('channel_cap/d')?:null,                    //容量限制
                'master_swt_id' => trim($this->request->post('master_swt_id'))?:null,                //*主切换台ID
                'slave_swt_id' => trim($this->request->post('slave_swt_id'))?:null,                  //*备切换台ID
                'master_vdr_id' => trim($this->request->post('master_vdr_id'))?:null,                //*主视频服务器ID
                'master_vdr_chan' => trim($this->request->post('master_vdr_chan'))?:null,            //*主视频服务器通道
                'slave_vdr_id' => trim($this->request->post('slave_vdr_id'))?:null,                  //*备视频服务器ID
                'slave_vdr_chan' => trim($this->request->post('slave_vdr_chan'))?:null,              //*备视频服务器通道
                'master_play_term' => trim($this->request->post('master_play_term'))?:null,          //*主播出站终端ID
                'slave_play_term' => trim($this->request->post('slave_play_term'))?:null,            //*备播出站终端ID
                's_play_time' => $this->request->post('s_play_time/d')?:null,                        //开播时间
                'keymix' => trim($this->request->post('keymix'))?:null,                              //*外置键混
                'cg' => trim($this->request->post('cg'))?:null,                                      //*控制字幕机
                'master_heartbeat_port' => $this->request->post('master_heartbeat_port/d')?:null,//*主心跳端口
                'slave_heartbeat_port' => $this->request->post('slave_heartbeat_port/d')?:null,  //*备心跳端口
                'remote_port' => $this->request->post('remote_port/d')?:null,                    //远程端口
                'remote_mode' => trim($this->request->post('remote_mode'))?:null,                //远程模式
                'remark' => $this->request->post('remark')?:null,                              //备注
                'adv_chann_id' => $this->request->post('adv_chann_id/d')?:null,                  //
                'InsPlay_flag' => $this->request->post('InsPlay_flag/d')?:null,                  //
                'InsPlay_seg' => $this->request->post('InsPlay_seg/d')?:null,                    //
                'aircg_chann_id' => $this->request->post('aircg_chann_id/d')?:null,              //
                'is_24_hours' => $this->request->post('is_24_hours/d')?:null,                    //是否24小时持续播放
                'manu_load_pgm' => $this->request->post('manu_load_pgm/d')?:null,                //
                'pgm_build_mode' => $this->request->post('pgm_build_mode/d')?:null,              //
                'channel_adv_cap' => $this->request->post('channel_adv_cap/d')?:null,            //
                'is_thirdbk' => $this->request->post('is_thirdbk/d')?:null,                      //是否有第三备
                'is_upplay' => $this->request->post('is_upplay/d')?:null,                            //是否提前播
                'third_play_term' => trim($this->request->post('third_play_term'))?:null,            //第三播出站终端ID
                'third_vdr_id' => trim($this->request->post('third_vdr_id'))?:null,                  //第三播出服务器ID
                'third_vdr_chan' => trim($this->request->post('third_vdr_chan'))?:null,              //第三备播出通道
                'upplay_time' => $this->request->post('upplay_time/d')?:null,                        //提前播时间
                'upplay_vdr_id' => trim($this->request->post('upplay_vdr_id'))?:null,                //提前播视频服务器ID
                'upplay_vdr_chan' => trim($this->request->post('upplay_vdr_chan'))?:null,            //提前播播出通道
                'InsPlay_AdvGrp' => $this->request->post('InsPlay_AdvGrp/d')?:null,                  //
                'Video_Format' => $this->request->post('Video_Format/d')?:null,                      //
//                'create_time' => date('Y-m-d H:i:s',time()),                                 //*添加时间
//                'up_time' => date('Y-m-d H:i:s',time()),                                     //*更新时间
            ]);
        }
    }

    /**
     *获取所有频道列表
     */
    public function channelAllList(){
        $param = $this->request->get([
            'per_page' => $this->request->get('pagenum/d')?:1,
            'pagesize' => $this->request->get('pagesize/d')?:100,
            'channel_name' => trim($this->request->get('channel_name'))?:null,
        ]);
        $param['offset'] = ($param['per_page']-1) * $param['pagesize'];
        $list = ChannelInfo::channelList($param);
        return successJson($list, '获取成功');
    }

    /**
     *获取频道详情
     */
    public function channelInfo(){
        $param = $this->request->get([
            'id' => $this->request->get('id/d')?:null,
        ]);
        try {
            $data = ChannelInfo::channelInfo($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    /**
     *频道添加
     */
    public function channelAdd(){
        try {
            $this->validate($this->channelParam, 'app\common\validate\Channel.addsave');
            $data = ChannelInfo::channelAdd($this->channelParam);
        } catch (ValidateException $e) {
            return validateJson($e->getError(),$e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    /**
     *频道编辑
     */
    public function channelEdit(){
        try {
            $this->validate($this->channelParam, 'app\common\validate\Channel.addedit');
            $data = ChannelInfo::channelEdit($this->channelParam);
        } catch (ValidateException $e) {
            return validateJson($e->getError(),$e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    /**
     *频道删除
     */
    public function channelDel(){
        $param = $this->request->post([
            'id' => $this->request->post('id/d')?:null,
        ]);
        try {
            $data = ChannelInfo::channelDel($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

}